package cn.zh.npro.Activity;

import android.graphics.Color;
import android.support.annotation.Nullable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;

import com.github.mikephil.charting.formatter.XAxisValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;

import com.github.mikephil.charting.utils.ViewPortHandler;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.zh.npro.R;
import cn.zh.npro.Util.BaseUrl;

import cn.zh.npro.Util.MyMarkerView;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class Gooddw extends BaseActivity{
  ImageView gdback;
  LineChart mLineChart;
  String name,gid;
  TextView proname,gwempty;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gooddw);
        name=getIntent().getStringExtra("name");
        gid=getIntent().getStringExtra("gid");
        initgt();
        getGdtail(gid);
        }

    public void initgt(){
        gdback=findViewById(R.id.gdback);
        proname=findViewById(R.id.proname);
        proname.setText(name+"(销售报表)");
        gwempty=findViewById(R.id.gwempty);
        mLineChart=findViewById(R.id.gw_chart);
        gdback.setOnClickListener(this);

        }


    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id=view.getId();
        switch (id){
            case R.id.gdback:
                finish();
                break;
          }
        }


    private void initLineChart(final ArrayList<String> strings, ArrayList<Float> floats){

        List<Entry> entries = new ArrayList<>();
        entries.clear();
        for (int i = 0; i < floats.size(); i++) {
            float data = floats.get(i);
            entries.add(new Entry(data, i));
        }

        LineDataSet dataSet = new LineDataSet(entries, "");
        dataSet.setColor(Color.parseColor("#45a6d9"));// 每个点之间线的颜色，还有其他几个方法，自己看

        ArrayList<ILineDataSet> lineDataSets = new ArrayList<ILineDataSet>();
        lineDataSets.add(dataSet); // add the datasets

        LineData lineData = new LineData(strings,lineDataSets);
        lineData.setValueTextSize(15);
        dataSet.setLineWidth(2.0f);
        dataSet.setCircleSize(4f);
        dataSet.setColor(Color.parseColor("#3498DB"));
        dataSet.setCircleColor(Color.parseColor("#3498DB"));
        dataSet.setCircleColorHole(Color.parseColor("#3498DB"));
        mLineChart.setDescription(null);
        lineData.setDrawValues(false);
        mLineChart.setBackgroundColor(Color.WHITE);
        mLineChart.setNoDataText("暂无数据");
        mLineChart.setDrawGridBackground(true);
        mLineChart.setGridBackgroundColor(Color.WHITE);
        mLineChart.setDrawBorders(false);
        mLineChart.setTouchEnabled(true);
        mLineChart.setDragEnabled(true);
        mLineChart.setScaleEnabled(true);
        mLineChart.setScaleXEnabled(true);
        mLineChart.setScaleYEnabled(false);
        mLineChart.setPinchZoom(true);
        mLineChart.setDoubleTapToZoomEnabled(false);
        mLineChart.setDragDecelerationEnabled(true);
        mLineChart.setDragDecelerationFrictionCoef(0.9f);
        dataSet.setHighlightEnabled(true);
        dataSet.setDrawHighlightIndicators(false);
        XAxis xAxis = mLineChart.getXAxis();
        YAxis yAxis = mLineChart.getAxisLeft();
        mLineChart.getAxisRight().setEnabled(false);
        float ratio = (float) strings.size()/(float) 6;
        if (ratio>2.0){
            ratio=2;
        }
        mLineChart.zoom(ratio,1f,0,0);
        mLineChart.animateX(1500);

        yAxis.setAxisLineColor(Color.WHITE);
        xAxis.setDrawGridLines(false);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        yAxis.setLabelCount(3, false);
        yAxis.setAxisMinValue(0);

        xAxis.setValueFormatter(new XAxisValueFormatter() {
            @Override
            public String getXValue(String s, int i, ViewPortHandler viewPortHandler) {
                String year=s.substring(0,4);
                String yue=s.substring(4);
                return year+"-"+yue;
            }
        });
        Legend l = mLineChart.getLegend();
        l.setEnabled(false);

        mLineChart.setMarkerView(new MyMarkerView(this, R.layout.custom_marker_view));    // 点击数据点显示的pop，有俩默认的，MarkerImage：一张图片，MarkerView:一个layout布局,也可以自己定义.这里这个是我自定义的一个MarkerView。
        mLineChart.setData(lineData);
        if (mLineChart.getData().getYMax()==0){
            mLineChart.getAxisLeft().setAxisMaxValue(20f);
            mLineChart.notifyDataSetChanged();
        }
       }




    public void getGdtail(String gid){
        OkHttpUtils.get(BaseUrl.GetProductReport)
                .params("goodsId",gid)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        System.out.println("----------------->销售额=="+s);
                        try {
                            JSONObject js=new JSONObject(s);
                            boolean State=js.getBoolean("State");
                            if (State){
                                JSONArray Data=js.getJSONArray("Data");

                                ArrayList<String>date=new ArrayList<>();
                                ArrayList<Float> score= new ArrayList<>();
                                for (int i=0;i<Data.length();i++){
                                    JSONObject ji=Data.getJSONObject(i);
                                    String YearMonth=ji.getString("YearMonth");
                                    double SellCount=ji.getDouble("SellCount");
                                    date.add(YearMonth);
                                    score.add((float)SellCount);
                                   }
                                  if (score.size()==0){
                                      gwempty.setVisibility(View.VISIBLE);
                                      mLineChart.setVisibility(View.GONE);
                                  }else {
                                      gwempty.setVisibility(View.GONE);
                                      mLineChart.setVisibility(View.VISIBLE);
                                      initLineChart(date,score);
                                    }

                             }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                 });
              }


}
